//
//  TZNewUserController.m
//  wangYiLottery
//
//  Created by mac on 16/2/20.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZNewUserController.h"
#import "TZUserDelegateController.h"
#import "TZSuccessLoginController.h"

@interface TZNewUserController ()
@property (nonatomic, strong) UIView *phoneView;
@property (nonatomic, strong) UIView *verificationView;
@property (nonatomic, strong) UIButton *verificationButton;
@property (nonatomic, strong) UIView *lnviteView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *userButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *passwordLogin;
@property (nonatomic, strong) UITextField *phoneField;
@property (nonatomic, strong) UITextField *verField;
@property (nonatomic, strong) UITextField *lnvField;
@end
@implementation TZNewUserController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem setTitle:@"用户注册"];
    [self.view endEditing:YES];
    [self setupUI];
}

//跟新UI
- (void)setupUI {
    [self addPhoneNumberInController];
    [self addVerificationInController];
    [self addSettingVerificationInController];
    [self addLnviteCodeInCotroller];
    [self addUserDelegateInController];
    [self addNextButtonInController];
    [self addPasswordLoginInController];
}

//电话号码
- (void)addPhoneNumberInController {
    self.phoneView = [[UIView alloc] init];
    [self.view addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(30);
        make.width.mas_equalTo(KSCREEN_WIDTH * 0.8);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(40);
    }];
    //内嵌整体图片
    UIImageView *wholeImageView = [[UIImageView alloc] init];
    [self.phoneView addSubview:wholeImageView];
    [wholeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.phoneView);
    }];
    wholeImageView.image = [UIImage imageNamed:@"yuan"];
    //内嵌手机图片
    UIImageView *thumbnailImageView = [[UIImageView alloc] init];
    [wholeImageView addSubview:thumbnailImageView];
    [thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wholeImageView.mas_left).offset(11);
        make.height.mas_equalTo(18.5);
        make.width.mas_equalTo(18);
        make.centerY.equalTo(wholeImageView.mas_centerY);
    }];
    thumbnailImageView.image = [UIImage imageNamed:@"shouji"];
    //内嵌textField
    UITextField *field = [[UITextField alloc] init];
    [self.phoneView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thumbnailImageView.mas_right).offset(7);
        make.right.equalTo(wholeImageView.mas_right).offset(-8);
        make.centerY.equalTo(wholeImageView.mas_centerY);
        make.height.mas_equalTo(thumbnailImageView.mas_height);
    }];
    field.placeholder = @"请输入手机号码";
    field.font = [UIFont systemFontOfSize:12.0];
    field.textColor = [UIColor blackColor];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneField = field;
}

//验证码
- (void)addVerificationInController {
    self.verificationView = [[UIView alloc] init];
    [self.view addSubview:self.verificationView];
    [self.verificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneView.mas_bottom).offset(20);
        make.left.equalTo(self.phoneView.mas_left);
        make.height.mas_equalTo(self.phoneView.mas_height);
        make.width.mas_equalTo(self.phoneView.mas_width).multipliedBy(0.75);
    }];
    //内嵌整体图片
    UIImageView *wholeImageView = [[UIImageView alloc] init];
    [self.verificationView addSubview:wholeImageView];
    [wholeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.verificationView);
    }];
    wholeImageView.image = [UIImage imageNamed:@"yuan"];
    //内嵌手机图片
    UIImageView *thumbnailImageView = [[UIImageView alloc] init];
    [wholeImageView addSubview:thumbnailImageView];
    [thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wholeImageView.mas_left).offset(11);
        make.height.mas_equalTo(18.5);
        make.width.mas_equalTo(18);
        make.centerY.equalTo(wholeImageView.mas_centerY);
    }];
    thumbnailImageView.image = [UIImage imageNamed:@"mima"];
    //内嵌textField
    UITextField *field = [[UITextField alloc] init];
    [self.verificationView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thumbnailImageView.mas_right).offset(7);
        make.right.equalTo(wholeImageView.mas_right).offset(-8);
        make.centerY.equalTo(wholeImageView.mas_centerY);
        make.height.mas_equalTo(thumbnailImageView.mas_height);
    }];
    field.placeholder = @"请输入验证码";
    field.font = [UIFont systemFontOfSize:12.0];
    field.textColor = [UIColor blackColor];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.verField = field;
}

//获取验证码
- (void)addSettingVerificationInController {
    self.verificationButton = [[UIButton alloc] init];
    [self.view addSubview:self.verificationButton];
    [self.verificationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.verificationView);
        make.height.mas_equalTo(self.verificationView.mas_height);
        make.right.equalTo(self.phoneView.mas_right);
        make.width.mas_equalTo(self.phoneView.mas_width).multipliedBy(0.2);
    }];
    [self.verificationButton setBackgroundImage:[UIImage imageNamed:@"yuanjiao1"] forState:UIControlStateNormal];
    [self.verificationButton setTitle:@"验证码" forState:UIControlStateNormal];
    [self.verificationButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.verificationButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [self.verificationButton addTarget:self action:@selector(verificationButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//获取验证码监听事件
- (void)verificationButtonDidClick {
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (error) {
            if (self.phoneField.text.length != 11) {
                [SVProgressHUD showWithStatus:@"手机号输入有误"];
            } else {
                [SVProgressHUD showWithStatus:@"验证码输入有误"];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        } else {
            [self.verificationButton startWithTime:60 title:nil countDownTitle:@"s" mainColor:nil countColor:nil];
        }
    }];
}


//邀请码
- (void)addLnviteCodeInCotroller {
    self.lnviteView = [[UIView alloc] init];
    [self.view addSubview:self.lnviteView];
    [self.lnviteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.verificationView.mas_bottom).offset(20);
        make.height.width.mas_equalTo(self.phoneView);
        make.centerX.equalTo(self.phoneView.mas_centerX);
    }];
    //内嵌整体图片
    UIImageView *wholeImageView = [[UIImageView alloc] init];
    [self.lnviteView addSubview:wholeImageView];
    [wholeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.lnviteView);
    }];
    wholeImageView.image = [UIImage imageNamed:@"yuan"];
    //内嵌手机图片
    UIImageView *thumbnailImageView = [[UIImageView alloc] init];
    [wholeImageView addSubview:thumbnailImageView];
    [thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wholeImageView.mas_left).offset(11);
        make.height.mas_equalTo(18.5);
        make.width.mas_equalTo(18);
        make.centerY.equalTo(wholeImageView.mas_centerY);
    }];
    thumbnailImageView.image = [UIImage imageNamed:@"mima"];
    //内嵌textField
    UITextField *field = [[UITextField alloc] init];
    [self.lnviteView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thumbnailImageView.mas_right).offset(7);
        make.right.equalTo(wholeImageView.mas_right).offset(-8);
        make.centerY.equalTo(wholeImageView.mas_centerY);
        make.height.mas_equalTo(thumbnailImageView.mas_height);
    }];
    field.placeholder = @"请输入邀请码 (选填)";
    field.font = [UIFont systemFontOfSize:12.0];
    field.textColor = [UIColor blackColor];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
}

//用户协议
- (void)addUserDelegateInController {
    //可选中按钮
    self.iconView = [[UIImageView alloc] init];
    [self.view addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lnviteView.mas_bottom).offset(20);
        make.left.equalTo(self.lnviteView.mas_left);
        make.width.height.mas_equalTo(10);
    }];
    self.iconView.image = [UIImage imageNamed:@"xuanHeight"];
    //协议文字
    self.titleLabel = [[UILabel alloc] init];
    [self.view addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_top);
        make.left.equalTo(self.iconView.mas_right).offset(4);
        make.width.mas_equalTo(74);
    }];
    self.titleLabel.text = @"本人已阅读并同意";
    self.titleLabel.font = [UIFont systemFontOfSize:9.0];
    self.titleLabel.textColor = [UIColor blackColor];
    //用户协议按钮
    self.userButton = [[UIButton alloc] init];
    [self.view addSubview:self.userButton];
    [self.userButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right);
        make.top.equalTo(self.titleLabel.mas_top);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(37);
    }];
    [self.userButton setTitle:@"用户协议" forState:UIControlStateNormal];
    [self.userButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.userButton.titleLabel.font = [UIFont systemFontOfSize:9.0];
    [self.userButton addTarget:self action:@selector(userButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//用户协议按钮监听事件
- (void)userButtonDidClick {
    TZUserDelegateController *userDelegateController = [[TZUserDelegateController alloc] init];
    [self.navigationController pushViewController:userDelegateController animated:YES];
}

//下一步
- (void)addNextButtonInController {
    self.nextButton = [[UIButton alloc] init];
    [self.view addSubview:self.nextButton];
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.phoneView);
        make.left.equalTo(self.phoneView);
        make.top.equalTo(self.iconView.mas_bottom).offset(20);
    }];
    [self.nextButton setBackgroundImage:[UIImage imageNamed:@"RedButton"] forState:UIControlStateNormal];
    [self.nextButton setBackgroundImage:[UIImage imageNamed:@"RedButtonPressed"] forState:UIControlStateHighlighted];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(nextButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//下一步监听事件
- (void)nextButtonDidClick {
    //判断手机号是否输入正确
    if (self.phoneField.text.length != 11) {
        [SVProgressHUD showWithStatus:@"手机号码格式错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    //判断验证码是否写入值
    if (self.verField.text.length == 0) {
        [SVProgressHUD showWithStatus:@"验证码格式错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    //判断验证码是否输入正确
    [SMSSDK commitVerificationCode:self.verField.text phoneNumber:self.phoneField.text zone:@"86" result:^(NSError *error) {
        if (error) {
            [SVProgressHUD showWithStatus:@"验证码有误"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            return ;
        } else {
            TZSuccessLoginController *successLoginController = [[TZSuccessLoginController alloc] init];
            [self.navigationController pushViewController:successLoginController animated:YES];
        }
    }];
}

//已有用户，直接登录
- (void)addPasswordLoginInController {
    self.passwordLogin = [[UIButton alloc] init];
    [self.view addSubview:self.passwordLogin];
    [self.passwordLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nextButton.mas_bottom).offset(36);
        make.centerX.equalTo(self.phoneView.mas_centerX);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(130);
    }];
    [self.passwordLogin setTitle:@"已有注册账户 直接登录" forState:UIControlStateNormal];
    [self.passwordLogin setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.passwordLogin.titleLabel.font = [UIFont systemFontOfSize:9.0];
    [self.passwordLogin addTarget:self action:@selector(passwordLoginDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//直接登录监听事件
- (void)passwordLoginDidClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
